package com.lvmama.dashboard.core.aop;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.lvmama.dashboard.core.rop.AjaxMethodHandler;
import com.lvmama.dashboard.core.rop.AjaxRequestContext;
import com.lvmama.dashboard.core.rop.AjaxServiceHandler;
import com.lvmama.dashboard.model.rop.AjaxRequest;
import com.lvmama.dashboard.model.rop.AjaxRequestBody;
import com.lvmama.dashboard.utils.ServletUtils;

public class AjaxContextInterceptor extends HandlerInterceptorAdapter {
	
	private static Logger logger = Logger.getLogger(AjaxContextInterceptor.class);
	@Autowired
	private AjaxServiceHandler ajaxServiceHandler;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String method = request.getParameter("ajaxmethod");
		logger.debug("ajaxmethod>>>>>>>>>>>>>>>>>" + method);
		AjaxMethodHandler mehtodHandler  = ajaxServiceHandler.getHandler(method);
		AjaxRequestBody<?> requestBody = buildParams(mehtodHandler, request, response);
		ServletContext servletContext = request.getSession().getServletContext();
		logger.debug(servletContext);
		AjaxRequestContext arc = new AjaxRequestContext();
		arc.setServiceHandler(mehtodHandler);
		arc.setRequestData(requestBody);
		arc.setRequest(request);
		arc.setResponse(response);
		servletContext.setAttribute("ajaxRequestContext", arc);
		return true;
	}
	
	private AjaxRequestBody<?> buildParams(AjaxMethodHandler handler, HttpServletRequest request, HttpServletResponse response) throws InstantiationException, IllegalAccessException {
		Type[] typs = handler.getHandlerMethod().getGenericParameterTypes();
		Type t = typs[0];
		Class<?> obj = null;
		// 如果是泛型类型
		if (t instanceof ParameterizedType) {
			Type[] types = ((ParameterizedType) t).getActualTypeArguments();// 泛型类型列表
			for (Type type : types) {
				obj = (Class<?>) type;
			}
		}
		Object object = obj;
		object = obj.newInstance();

		final AjaxRequest requestBody = (AjaxRequest) object;
		if (object instanceof AjaxRequest) {
			AjaxRequest ropRequest = (AjaxRequest) object;
			ropRequest.setRequest(request);
			ropRequest.setResponse(response);
		}

		DateConverter dateConverter = new DateConverter();
		ConvertUtils.register(dateConverter, Date.class);// 注册㈠个日期类
		AjaxRequestBody<Object> body = null;
		try {
			Map<?, ?> parameterMap = request.getParameterMap();
			BeanUtils.populate(requestBody, parameterMap);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		String logsessionid = ServletUtils.getLogsessionid(request, response);
		requestBody.setLogsessionid(logsessionid);
		body = new AjaxRequestBody<Object>();
		body.setT(requestBody);
		return body;
	}
}
